package edu.hm.risikomanagement.library;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class DatabaseData {

	private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			+ "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/";

	private static byte[] zeroPad(int length, byte[] bytes) {
		byte[] padded = new byte[length];
		System.arraycopy(bytes, 0, padded, 0, bytes.length);
		return padded;
	}

	public static String encode(String string) {

		String encoded = "";
		byte[] stringArray;
		try {
			stringArray = string.getBytes("UTF-8");
		} catch (Exception ignored) {
			stringArray = string.getBytes();
		}
		int paddingCount = (3 - (stringArray.length % 3)) % 3;
		stringArray = zeroPad(stringArray.length + paddingCount, stringArray);
		for (int i = 0; i < stringArray.length; i += 3) {
			int j = ((stringArray[i] & 0xff) << 16)
					+ ((stringArray[i + 1] & 0xff) << 8)
					+ (stringArray[i + 2] & 0xff);
			encoded = encoded + base64code.charAt((j >> 18) & 0x3f)
					+ base64code.charAt((j >> 12) & 0x3f)
					+ base64code.charAt((j >> 6) & 0x3f)
					+ base64code.charAt(j & 0x3f);
		}
		return encoded.substring(0, encoded.length() - paddingCount)
				+ "==".substring(0, paddingCount);

	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static String deleteBenutzerFromDatabase(int id) {
		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/benutzerDelete.csv.php?id="
					+ id);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("getListFromDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("getListFromDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;

	}

	public static String deleteRisikoFromDatabase(int id) {
		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/risikoDelete.csv.php?id=" + id);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("getListFromDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("getListFromDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;

	}

	public static String getDataFromDatabase(String function) {
		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/" + function + ".csv.php");
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("getListFromDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("getListFromDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;

	}

	public static int getNextBenutzerId() {
		int id = 0;
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/benutzerNextId.csv.php");
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			id = Integer.parseInt(buff.readLine());

			// }

		} catch (MalformedURLException e) {
			Log.e("getNextId", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("getNextId", "Can't read from the Internet: " + e1.toString());
		}

		return id;

	}

	public static int getNextRisikoId() {
		int id = 0;
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/risikoNextId.csv.php");
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			id = Integer.parseInt(buff.readLine());

			// }

		} catch (MalformedURLException e) {
			Log.e("getNextId", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("getNextId", "Can't read from the Internet: " + e1.toString());
		}

		return id;

	}

	public static String insertBenutzerIntoDatabase(String s) {
		String queryString = encode(s);

		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/benutzerInsert.csv.php?"
					+ queryString);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("insertIntoDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("insertIntoDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;
	}

	public static String insertRisikoIntoDatabase(String s) {
		String queryString = encode(s);

		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/risikoInsert.csv.php?"
					+ queryString);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("insertIntoDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("insertIntoDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;
	}

	public static String selectBenutzerFromDatabase(int id) {
		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/benutzerSelect.csv.php?id="
					+ id);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("selectFromDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("selectFromDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;

	}

	public static String selectRisikoFromDatabase(int id) {
		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/risikoSelect.csv.php?id=" + id);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("selectFromDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("selectFromDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;

	}

	public static String updateBenutzerInDatabase(Integer id, String s) {
		String queryString = encode(id + "@@@" + s);

		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/benutzerUpdate.csv.php?"
					+ queryString);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("updateDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("updateDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;
	}

	public static String updateRisikoInDatabase(Integer id, String s) {
		String queryString = encode(id + "@@@" + s);

		String line = "";
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader inStream = null;
		BufferedReader buff = null;
		try {
			url = new URL("http://hm.tfonfara.de/risikoUpdate.csv.php?"
					+ queryString);
			urlConn = url.openConnection();
			inStream = new InputStreamReader(urlConn.getInputStream());
			buff = new BufferedReader(inStream);

			// if (buff.readLine() != null) {

			line = buff.readLine();

			// }

		} catch (MalformedURLException e) {
			Log.e("updateDatabase", "Please check the URL:" + e.toString());
		} catch (IOException e1) {
			Log.e("updateDatabase",
					"Can't read from the Internet: " + e1.toString());
		}

		return line;
	}

}
